package com.maidou.qiantai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.maidou.qiantai.dto.SpuAlterDto;
import com.maidou.qiantai.dto.SpuDto;
import com.maidou.qiantai.vo.SpuVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.Spu;
import com.maidou.qiantai.service.ISpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author maidou
 * @date 2024-03-20
 */
@RestController
@RequestMapping("/qiantai/spu")
public class SpuController extends BaseController
{
    @Autowired
    private ISpuService spuService;




    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(Spu spu)
    {
        startPage();
        List<SpuVo> list = spuService.selectSpuList(spu);
        SpuVo spuVo = list.get(0);
        list.remove(0);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(spuVo.getTotal());
        return dataTable;
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spu spu)
    {
        List<SpuVo> list = spuService.selectSpuList(spu);
        ExcelUtil<SpuVo> util = new ExcelUtil<SpuVo>(SpuVo.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spuService.selectSpuById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpuDto spuDto)
    {
        return toAjax(spuService.insertSpuDto(spuDto));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpuAlterDto spuAlterDto)
    {
        return toAjax(spuService.updateSpuDto(spuAlterDto));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:edit')")
    @Log(title = "商品上架状态", businessType = BusinessType.UPDATE)
    @PutMapping("/alter/{id}/{status}")
    public AjaxResult editStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status)
    {
        return toAjax(spuService.updateSpuStatus(id,status));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spuService.deleteSpuByIds(ids));
    }
}
